package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.OrderConvert;
import com.mcxx.modules.miniSalvation.order.dao.write.NoticeBoardDetailWriteDao;
import com.mcxx.modules.miniSalvation.order.dao.write.NoticeBoardWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.NoticeBoardDTO;
import com.mcxx.modules.miniSalvation.order.dto.NoticeBoardDetailDTO;
import com.mcxx.modules.miniSalvation.order.entity.NoticeBoardEntity;
import com.mcxx.util.BatchNoUtil;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("noticeBoardWriteService")
@TaTransactional
public class NoticeBoardWriteService extends BaseService {
    @Autowired
    private NoticeBoardDetailWriteDao noticeBoardDetailWriteDao;
    @Autowired
    private NoticeBoardWriteDao noticeBoardWriteDao;


    public Boolean addNoticeBoard(NoticeBoardDTO noticeBoard, UserAccountVo userAccount){

        noticeBoard.setState(1);

        noticeBoard.setOrgId(userAccount.getOrgId());

        noticeBoard.setUserAreaLevel(userAccount.getAreaLevel());
        noticeBoard.setUserId(userAccount.getUserId());

        NoticeBoardEntity noticeBoardEntity = OrderConvert.convertNoticeBoardEntity(noticeBoard);
        if(StringUtils.isEmpty(noticeBoardEntity.getId())){
            noticeBoardEntity.setId(Constant.getUUID());
        }


        return noticeBoardWriteDao.insert(noticeBoardEntity);
    }


    public int updateStatusByBatchId(String batchId, String state, String finishRemark){
        return noticeBoardWriteDao.updateStatusByBatchId(batchId, state, finishRemark);
    }


    public int updateStatusByBatchNos(List<String> batchNoList, String state, String remark){
        return noticeBoardWriteDao.updateStatusByBatchNos(batchNoList, state, remark);
    }


    public int deleteBatch(List<String> batchNoList){

        noticeBoardDetailWriteDao.deleteByBatchNos(batchNoList);


        int rst = noticeBoardWriteDao.deleteByBatchNos(batchNoList);
        if(rst <= 0){
            throw new BizException("-1", "批次删除失败");
        }
        return rst;
    }


    public int deleteUpdateBatch(List<String> batchNoList){

        noticeBoardDetailWriteDao.deleteByBatchNos(batchNoList);


        int rst = noticeBoardWriteDao.updateStatusByBatchNos(batchNoList,"5", null);
        if(rst <= 0){
            throw new BizException("-1", "批次删除失败");
        }
        return rst;
    }


    public int updateStatusByBatchIds(List<String> batchNoList, String state){
        return noticeBoardWriteDao.updateChildNoticeByParNo(batchNoList, state);
    }

}
